//
//  HMCalendar.m
//  MotherBaby
//
//  Created by mengxiangyu on 2017/7/9.
//  Copyright © 2017年 mengxiangyu. All rights reserved.
//

#import "HMCalendar.h"
#import "HMCalendarModel.h"
#import "HMCalendarCell.h"
#import "HMCalendarFlowLayout.h"
// 宏定义设备尺寸
#define ScreenWH [UIScreen mainScreen].bounds.size


@interface HMCalendar ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (strong, nonatomic) NSMutableArray *calendarDataArray;

@end

@implementation HMCalendar

static NSString *kIdentifier = @"calendar";

#pragma mark - 初始化方法
+ (instancetype)calendar {
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化操作
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

#pragma mark - 懒加载
- (NSMutableArray *)calendarDataArray {
    if (!_calendarDataArray) {
        _calendarDataArray = [NSMutableArray array];
    }
    return _calendarDataArray;
}

- (void)showCalendarWithYear:(NSInteger)year month:(NSInteger)month {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy/MM"];
    NSDate *date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld/%ld", year, month]];
    NSInteger numberOfDays = [self numberOfDaysInDate:date];
    NSInteger firstDayOfWeek = [self firstDayOfWeekInDate:date];
    NSInteger lastNumberOfDays = [self numberOfDaysInLastDate:date];
    NSInteger today = [self today];
    // 计算多少行
    NSInteger rowNum = (numberOfDays - (7-(firstDayOfWeek-1)) + 6) / 7 + 1;
    
    XYLog(@"%ld------%ld------%ld------%ld", numberOfDays, firstDayOfWeek, rowNum, lastNumberOfDays);
    // 添加上个月剩余的那几天
    for (NSInteger i=1; i < firstDayOfWeek; i++) {
        HMCalendarModel *model = [[HMCalendarModel alloc] init];
        model.day = lastNumberOfDays-firstDayOfWeek+1+i;
        model.isShow = NO;
        model.monthType = 0;
        [self.calendarDataArray addObject:model];
    }
    
    // 添加当月天
    for (NSInteger i=1; i <= numberOfDays; i++) {
        HMCalendarModel *model = [[HMCalendarModel alloc] init];
        model.day = i;
        model.isShow = YES;
        model.monthType = 1;
        if (today==i) {
            model.isToday = YES;
        }
        [self.calendarDataArray addObject:model];
    }
    
    
    NSInteger count = 42-_calendarDataArray.count;
    // 添加下个月天数
    for (NSInteger i=1; i <= count; i++) {
        HMCalendarModel *model = [[HMCalendarModel alloc] init];
        model.day = i;
        model.isShow = NO;
        model.monthType = 2;
        [self.calendarDataArray addObject:model];
    }

    
    //    self.contentView.backgroundColor = [UIColor blueColor];
    
    //    CGFloat itemWidth = (_width-5*8) / 7 - 1;
//    CGFloat itemWidth = 40;
//    CGFloat itemHeight = itemWidth*0.9;
//
    
    HMCalendarFlowLayout *layout = [[HMCalendarFlowLayout alloc] init];
    // 添加collectionView
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, HM_WIDTH, HM_HEIGHT) collectionViewLayout:layout];
    
    // 告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中)
    [self.collectionView registerClass:[HMCalendarCell class] forCellWithReuseIdentifier:kIdentifier];
    self.collectionView.backgroundColor = Color_ControllerView;

    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;

    [self addSubview:_collectionView];
    
    
}

#pragma mark - UICollectionViewDelegate 及 UICollectionViewDataSource方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6*7;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HMCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIdentifier forIndexPath:indexPath];
    
    cell.model = self.calendarDataArray[indexPath.row];
    
    return cell;
}

#pragma mark - 日期获取相关
//获取目标月份的天数
- (NSInteger)numberOfDaysInDate:(NSDate *)date
{
    //获取选中日期月份的天数
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
}

//获取目标月份第一天星期几
- (NSInteger)firstDayOfWeekInDate:(NSDate *)date
{
    //获取选中日期月份第一天星期几，因为默认日历顺序为“日一二三四五六”，所以这里返回的1对应星期日，2对应星期一，依次类推
    return [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfYear forDate:date];
}

// 获取上个月天数
- (NSInteger)numberOfDaysInLastDate:(NSDate *)date {
    NSDateComponents *cmp = [[NSCalendar currentCalendar] components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];
    //设置上个月，即在现有的基础上减去一个月(2017年1月 减去一个月 会得到2016年12月)
    [cmp setMonth:[cmp month] - 1];
    NSDate *lastMonDate = [[NSCalendar currentCalendar] dateFromComponents:cmp];
    return [self numberOfDaysInDate:lastMonDate];
}

// 获取今日
- (NSInteger)today {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd"];
    NSString *dateTime = [formatter stringFromDate:[NSDate date]];
    return [dateTime integerValue];
}

@end
